package com.login;


import java.util.HashMap;
import java.util.Vector;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public abstract class LoginAlphaAdapter extends BaseAdapter {
	
	private Vector<LoginElement> allData = new Vector<LoginElement>();
	
	public LoginAlphaAdapter(HashMap<String, Vector<LoginElement>> data)
	{
		loadData(data);
	}
	
	public void loadData(HashMap<String, Vector<LoginElement>> data)
	{
		allData.clear();
		
		String alphaText = "#";
		if(data.containsKey(alphaText))
		{
			allData.add(new LoginElement(alphaText,null, null));
			for(LoginElement le : data.get(alphaText))
			{
				allData.add(le);
			}
		
		}
		
		for(int i=1; i < 27; i++){
			alphaText = String.valueOf((char)(65 + (i-1)));
			if(data.containsKey(alphaText))
			{
				allData.add(new LoginElement(alphaText,null, null));
				for(LoginElement le : data.get(alphaText))
				{
					allData.add(le);
				}
			
			}
		}
	}
	
	protected abstract View getMainView(View convertView, boolean header, String textTop, String textCenter, String textBottom); 
	
	
	public int getCount() {
		return allData.size();
	}

	public Object getItem(int position) {
		return allData.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) 
	{
		LoginElement le = allData.get(position);
		if(le.value == null)
			return getMainView(convertView, true, le.name, "", "");
		else
			return getMainView(convertView, false, le.name, le.key, le.value);
		
	}
	
	public static class LoginElement implements Comparable<LoginElement>{

		public String name;
		public String key;
		public String value;
		
		public LoginElement(String name, String key, String value) {
			this.name = name;
			this.key = key;
			this.value = value;
		}

		public int compareTo(LoginElement o) {
			return this.name.compareTo(o.name);
		}
	}

}
